package com.juno.cafenoa.menu;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.juno.cafenoa.IntroActivity;
import com.juno.cafenoa.MainActivity;

public class MenuBasicItem {
	int productNumber;
	int parentCategoryNumber;
	String productName;
	String productDetailInfo;
	int price;
	String imgUrl;
	boolean isAddedInCart = false;
	String productOption;
	Context mContext;

	public MenuBasicItem(int id, int parentCategoryNumber, String productName,
			int price, String imgUrl, String content, String option) {
		this.productNumber = id;
		this.parentCategoryNumber = parentCategoryNumber;
		this.productName = productName;
		this.price = price;
		this.imgUrl = imgUrl;
		this.productDetailInfo = content;
		this.productOption = option;
	}

	public MenuBasicItem(Context mContext, int id, int parentCategoryNumber,
			String productName, int price, String imgUrl, String content, String option) {
		this.mContext = mContext;
		this.productNumber = id;
		this.parentCategoryNumber = parentCategoryNumber;
		this.productName = productName;
		this.price = price;
		this.imgUrl = imgUrl;
		this.productDetailInfo = content;
		this.productOption = option;
		
		MainActivity.executor.execute(downloadImage);
	}

	public MenuBasicItem(String productName) {
		this.productName = productName;
	}

	public MenuBasicItem(String productName, String productDetailInfo) {
		this.productName = productName;
		this.productDetailInfo = productDetailInfo;
	}

	public MenuBasicItem(String imgUrl, String productName, int price,
			boolean isAddedInCart) {
		this.imgUrl = imgUrl;
		this.productName = productName;
		this.price = price;
		this.isAddedInCart = isAddedInCart;
	}

	Runnable downloadImage = new Runnable() {
		Bitmap image;
		@Override
		public void run() {
			
			try {
				URL imageURL = new URL(imgUrl);
				HttpURLConnection conn = (HttpURLConnection) imageURL
						.openConnection();
				conn.setConnectTimeout(3000);
				conn.setReadTimeout(3000);
				conn.connect();


				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					int imgSize = conn.getContentLength();
					if (imgSize > 0) {
						BufferedInputStream bis = new BufferedInputStream(
								conn.getInputStream(), imgSize);
						Bitmap bm = BitmapFactory.decodeStream(bis);
						bis.close();
						image = bm;
					}
				}
			} catch (Exception ex) {
				// 기본이미지 불러오는 루틴. 추후 추가예정.
				InputStream is = IntroActivity.class.getClassLoader()
						.getResourceAsStream(
								"com/juno/cafenoa/res/temp_stub.png");

				if (is != null) {
					image = BitmapFactory.decodeStream(is);
					try {
						is.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}

			// Bitmap 이미지를 캐시이미지로 저장하는 루틴 추가.
			// Bitmap을 캐시메모리로 저장하는 루틴.
			String filename = String.valueOf(parentCategoryNumber) + "-" + String.valueOf(productNumber)+".png";
			
			try {
				File cacheDir = mContext.getCacheDir();
				File cacheFile = new File(cacheDir.getAbsolutePath(), filename);
				FileOutputStream fos = new FileOutputStream(cacheFile.getAbsolutePath());
				image.compress(CompressFormat.PNG, 100, fos);
				fos.flush();
				fos.close();
				Log.v("filename","success to save image");
			} catch (FileNotFoundException fnfe) {
				fnfe.printStackTrace();
				Log.v("???","file not exception : ", fnfe);
			} catch (IOException ie) {
				ie.printStackTrace();
				Log.v("???","io exception : ", ie);
			}
		}
	};

	public int getProductNumber() {
		return productNumber;
	}

	public void setProductNumber(int productNumber) {
		this.productNumber = productNumber;
	}

	public int getParentCategoryNumber() {
		return parentCategoryNumber;
	}

	public void setParentCategoryNumber(int parentCategoryNumber) {
		this.parentCategoryNumber = parentCategoryNumber;
	}

	public String getProductName() {
		return productName;
	}

	public String getProductOption() {
		return productOption;
	}

	public void setProductOption(String productOption) {
		this.productOption = productOption;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductDetailInfo() {
		return productDetailInfo;
	}
	
	

	public void setProductDetailInfo(String productDetailInfo) {
		this.productDetailInfo = productDetailInfo;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public boolean isAddedInCart() {
		return isAddedInCart;
	}

	public void setAddedInCart(boolean isAddedInCart) {
		this.isAddedInCart = isAddedInCart;
	}

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	public Runnable getDownloadImage() {
		return downloadImage;
	}

	public void setDownloadImage(Runnable downloadImage) {
		this.downloadImage = downloadImage;
	}

	

	
}
